package StudyCode.summary.zipfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UnZipFolder {
	public static void UnZipFolder(String zipFileString,
	                               String outPathString) throws Exception {
		ZipInputStream inZip = new ZipInputStream(new FileInputStream(
				zipFileString));
		ZipEntry zipEntry;
		String szName = "";
		while ((zipEntry = inZip.getNextEntry()) != null) {
			szName = zipEntry.getName();
			if (zipEntry.isDirectory()) {
				szName = szName.substring(0, szName.length() - 1);
				File folder = new File(outPathString + File.separator
						+ szName);
				folder.mkdirs();
			} else {
				File file = new File(outPathString + File.separator
						+ szName);
				if (file.getParentFile() != null
						&& !file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}

				file.createNewFile();
				FileOutputStream out = new FileOutputStream(file);
				int len;
				byte[] buffer = new byte[1024];
				while ((len = inZip.read(buffer)) != -1) {
					out.write(buffer, 0, len);
					out.flush();
				}
				out.close();
			}
		}
		inZip.close();
	}
}
